/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cdp.web.component.metadata.serializer;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.inspur.edp.cdp.common.utils.json.JsonUtil;
import com.inspur.edp.cdp.web.component.metadata.define.WebComponentMetadata;
import com.inspur.edp.lcm.metadata.api.IMetadataContent;
import com.inspur.edp.lcm.metadata.spi.MetadataContentSerializer;
import org.apache.commons.lang3.StringEscapeUtils;

import java.util.Objects;

/**
 * The type JsonSerializer
 *
 * @author: Zhibei Hao
 */
@Deprecated
public class JsonSerializer implements MetadataContentSerializer {
    @Override
    public JsonNode Serialize(IMetadataContent content) {
        if (Objects.isNull(content)) {
            return null;
        }
        WebComponentMetadata webCmpMetadata = (WebComponentMetadata) content;
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node =
                mapper
                        .configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
                        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                        .convertValue(webCmpMetadata, JsonNode.class);
        return node;
    }

    @Override
    public IMetadataContent DeSerialize(JsonNode node) {
//        if (Objects.isNull(node)) {
//            return null;
//        }
//        String contentStr = null;
//        if (node instanceof ObjectNode) {
//            contentStr = node.toString();
//        }
//        if (node instanceof TextNode) {
//            contentStr = node.toString();
//            contentStr = StringEscapeUtils.unescapeJava(contentStr);
//            contentStr = contentStr.substring(1, contentStr.length() - 1);
//        }
//        WebComponentMetadata metadata = JsonUtil.toObject(contentStr, WebComponentMetadata.class);
        return new WebComponentMetadataSerializer().DeSerialize(node);
    }
}
